# Definition for a binary tree node.
from typing import Optional


class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right
class Solution:
    def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
        if root is None:
            return False
        return  self.dfs(root, targetSum, 0)
    
    def dfs(self, node, targetSum, sumVal):
        sumVal += node.val
        if node.left is None and node.right is None:
            return sumVal == targetSum
        if node.left is not None and self.dfs(node.left, targetSum, sumVal):
            return True
        if node.right is not None and self.dfs(node.right, targetSum, sumVal):
            return True
        return False